141B - Hopscotch - CodeForces Solution


geometry math *1400

Please click on ads to support us..

Python Code:

from math import ceil

a, x, y = map(int, input().split())
l = y // a + ceil((y / a) % 1)
if ((l > 1 and l % 2 == 1) and (x == 0 or x >= a or x <= -a)) or (
        (l == 1 or l % 2 == 0) and (x >= a / 2 or x <= -a / 2)) or y % a == 0:
    print("-1")
    exit(0)

if l == 1:
    print("1")
    exit(0)

if l % 2 == 0:
    ans = 2 + (l / 2 - 1) * 3
else:
    ans = 2 + ((l - 1) / 2 - 1) * 3
    ans += 2 if x > 0 else 1

print(str(int(ans)))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define t int t; scanf("%d",&t); while(t--)
typedef long long ll;
const ll mod = 1e9+7;
const ll inf = 1LL<<30;
const int N = 2e5+10;
map<int,int> mp1;
map<int,int>mp2;
vector<int>v;
set<int>ss;
int b[N];
int c[N];
int a[N];
int dfs=1e9+7;
int main()
{
    int x,y,a,i=0;
    bool f=1;
    int cnt=0;
    i=0;
    f=1;
    cin>>a>>x>>y;
    if(y%a==0)
    {
        cout<<-1<<endl;
        return 0;
    }
    if(a>y)
    {
        if(abs(2*x)<a)
            cout<<1<<endl;
        else cout<<-1<<endl;
        return 0;
    }

 cnt=0;
 y-=a;
    while((i+a)<y)
    {
        i+=a;
        if(f==1) cnt++;
        else cnt+=2;
        f=!f;


    }
if(f==1) cnt++;
else cnt+=2;

    if(f==1&&abs(2*x)<a)
   cout<<cnt+1<<endl;
  else if(f==0&&abs(x)<a&&x!=0)
  cout<<cnt+1-(x<0)<<endl;
    else cout<<-1<<endl;





}


Comments

Submit
0 Comments
More Questions

869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference